package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.BrowseDnTaskOrderInfo;
import com.tiza.xiaoyoubao.entity.BrowseTaskOrderInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by mapple on 17/4/10.
 * 电脑淘宝任务(浏览赚金)
 */

public class BrowseDnTaoBaoTaskFragment extends BaseStoreHouseRefreshRecyclerFragment {

    private static final  String LOGTAG = "BrowseDnTaoBaoTaskFragment";

  private List<BrowseTaskOrderInfo> infoList;
        @Override
        protected RecyclerBaseAdapter getListAdapter() {
            return new TaskListAdapter();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            selectOver();
            pageSize = Integer.MAX_VALUE;
        }

        @Override
        protected void onItemClick(View view, int position) {
            super.onItemClick(view, position);
            BrowseTaskOrderInfo info = (BrowseTaskOrderInfo) mAdapter.getData().get(position);
            Hawk.put(Constants.KEY_BROWSEMPTBTASKOEDER_INFO,info);
            Bundle args = new Bundle();
            args.putSerializable("info", info);
            UIHelper.showBrowseDnTaoBaoTaskDetial(mActivity,args);
            Log.v(LOGTAG, "scannSetting" + info.getScannSetting() + "----------" + "singleMuti" + info.getSingleMulti());
            finish();
        }

        public static class TaskListAdapter extends RecyclerBaseAdapter {
            private Integer cGood,cShopn,cAddGood = null;

            @Override
            protected View onCreateItemView(ViewGroup parent, int viewType) {
                return getLayoutInflater(parent.getContext()).inflate(
                        R.layout.item_browse_dn_taobao_task, parent, false
                );
            }

            @Override
            protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
                return new ViewHolder(viewType, view);
            }

            @Override
            public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
                super.onBindItemViewHolder(holder, position);
                final ViewHolder vh = (ViewHolder) holder;
                BrowseTaskOrderInfo info = (BrowseTaskOrderInfo) _data.get(position);
                vh.tv_money.setText(DateUtils.formateRate(info.getBaseCmi()));
                vh.tv_type.setText(info.getBackName());
                vh.tv_goodsType.setText(info.getTypeName());
                //vh.tv_taskNum.setText("剩余" + info.getTaskNumber() + "单");
                int resultType = checkMessage(info.getScannSetting().toString());
                Log.i(LOGTAG, "checkMessage" + resultType + "orderScann" + info.getOrderScann());
                //condition1-不传
                //condition2-传三张
                //condition3-添加购物车
                //condition4-收藏店铺
                //condition5-收藏商店
                //condition6-收藏商品、收藏店铺
                //condition7-收藏商品、添加购物车
                //condition8-收藏店铺、添加购物车
                if (!"1".equals(info.getOrderScann())) {
                    vh.tv_goodsType.setText("浏览");
                } else {
                    if (1 == resultType) {
                        vh.tv_goodsType.setText("浏览");
                    }
                    if (2 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏+购物车");
                    }
                    if (3 == resultType) {
                        vh.tv_goodsType.setText("浏览+购物车");
                    }
                    if (4 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏");
                    }
                    if (5 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏");
                    }
                    if (6 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏");
                    }
                    if (7 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏+购物车");
                    }
                    if (8 == resultType) {
                        vh.tv_goodsType.setText("浏览+收藏+购物车");
                    }
                }
                /*if (TextUtils.isEmpty(info.getTaskNumber())) {
                    vh.view1.setVisibility(View.GONE);
                    vh.view.setVisibility(View.VISIBLE);
                    vh.tv_taskId1.setText("ID:" + info.getUserId());
                    vh.tv_taskMoney1.setText(info.getPrice() + "元");
                } else {
                    vh.view.setVisibility(View.GONE);
                    vh.view1.setVisibility(View.VISIBLE);
                    vh.tv_taskNum.setText("剩余" + info.getTaskNumber() + "单");
                    vh.tv_taskId.setText("ID:" + info.getUserId());
                    vh.tv_taskMoney.setText(info.getPrice() + "元");
                }*/

            }

            private int checkMessage(String scannSetting) {
                Log.v(LOGTAG, scannSetting);
                if(null != scannSetting && !"".equals(scannSetting)) {
                    String replaceStr = scannSetting.replaceAll("=",":");
                    Log.v(LOGTAG, replaceStr);
                    try {
                        JSONObject jsonObject = new JSONObject(replaceStr);
                        cGood = jsonObject.getInt("c_good");
                        cShopn = jsonObject.getInt("c_shop");
                        cAddGood = jsonObject.getInt("c_add_good");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                boolean condition = (cGood != null && cShopn != null && cAddGood != null);
                boolean condition1 = (cGood == 0 && cShopn == 0 && cAddGood == 0);
                boolean condition2 = (cGood == 1 && cShopn == 1 && cAddGood == 1);
                boolean condition3 = (cGood == 0 && cShopn == 0 && cAddGood == 1);
                boolean condition4 = (cGood == 0 && cShopn == 1 && cAddGood == 0);
                boolean condition5 = (cGood == 1 && cShopn == 0 && cAddGood == 0);
                boolean condition6 = (cGood == 1 && cShopn == 1 && cAddGood == 0);
                boolean condition7 = (cGood == 1 && cShopn == 0 && cAddGood == 1);
                boolean condition8 = (cGood == 0 && cShopn == 1 && cAddGood == 1);
                if (condition) {
                    if (condition1) {
                        return 1;
                    }
                    if (condition2) {
                        return 2;
                    }
                    if (condition3) {
                        return 3;
                    }
                    if (condition4) {
                        return 4;
                    }
                    if (condition5) {
                        return 5;
                    }
                    if (condition6) {
                        return 6;
                    }
                    if (condition7) {
                        return 7;
                    }
                    if (condition8) {
                        return 8;
                    }
                }
                return 0;
            }

            static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

                @BindView(R.id.view)
                View view;
                @BindView(R.id.view1)
                View view1;
                @BindView(R.id.tv_taskId)
                TextView tv_taskId;
                @BindView(R.id.tv_taskId1)
                TextView tv_taskId1;
                @BindView(R.id.tv_taskMoney)
                TextView tv_taskMoney;
                @BindView(R.id.tv_taskMoney1)
                TextView tv_taskMoney1;
                @BindView(R.id.tv_money)
                TextView tv_money;
                @BindView(R.id.tv_time)
                TextView tv_time;
                @BindView(R.id.tv_type)
                TextView tv_type;
                @BindView(R.id.iv_money)
                ImageView iv_money;
                @BindView(R.id.tv_taskNum)
                TextView tv_taskNum;
                @BindView(R.id.tv_goodsType)
                TextView tv_goodsType;

                ViewHolder(int viewType, View view) {
                    super(viewType, view);
                    ButterKnife.bind(this, view);
                }
            }
        }

            @Override
            protected void requestData(boolean refresh) {
                super.requestData(refresh);
                ApiHttpClient.getTaskInfo(this, "10",
                        new JsonCallback<BrowseTaskOrderInfo>(BrowseTaskOrderInfo.class) {

                            @Override
                            public void onError(Call call, Exception e) {
                                onRequestError();
                            }

                            @Override
                            public void onResponse(ResponseInfo<BrowseTaskOrderInfo> response) {
                                int code = response.getCode();
                                if (code == 1) {
                                    mErrorLayout
                                            .setErrorType(EmptyLayout.NETWORK_ERROR);
                                    return;
                                }
                                infoList = response.getData();
                                onRequestResponse(infoList);
                            }

                            @Override
                            public void onAfter() {
                                super.onAfter();
                                onRequestFinish();
                            }
                        }
                );
            }

    private void selectOver() {
        ApiHttpClient.selectOver(this,
                new JsonCallback<String>(String.class, true) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("网络连接失败，请稍后重试");
                    }

                    @Override
                    public void onResponse(ResponseInfo<String> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        Map<String, Object> _map = response.getFirstEntity();
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }

    @Override
    public boolean onBackPressed() {
//        UIHelper.showBrowseMakeMoney(mActivity);
        finish();
        return true;
    }
}
